//
//  LSBaseViewController.swift
//  LSWeiBo0808
//
//  Created by 李明禄 on 2017/8/10.
//  Copyright © 2017年 SocererGroup. All rights reserved.
//

import UIKit

class LSBaseViewController: UIViewController {
    
    let naviBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width:UIScreen.main.bounds.width , height: 64))
    
    lazy var navItem = UINavigationItem()
    
    var tableview: UITableView?
    
    var downRefresh: UIRefreshControl?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        loadData()
        // Do any additional setup after loading the view.
    }
    
    override var title: String? {
        didSet {
            navItem.title = title
        }
    }

     func loadData() {  }

}

extension LSBaseViewController {
    func setupUI() {
        setupNavBar()
        setupTableView()
        self.view.backgroundColor = UIColor.cz_random()
        automaticallyAdjustsScrollViewInsets = false
    }
    
    private func setupTableView() {

        tableview = UITableView(frame: self.view.bounds, style: .plain)
        self.view.insertSubview(tableview!, belowSubview: naviBar)
        
        tableview?.delegate = self
        tableview?.dataSource = self
        
        tableview?.contentInset = UIEdgeInsets(top: naviBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        //添加刷新控件
        downRefresh = UIRefreshControl()
        tableview?.addSubview(downRefresh!)
        
        downRefresh?.addTarget(self, action: #selector(loadData), for: UIControlEvents.valueChanged)

    }
    
    private func setupNavBar() {
        self.view.addSubview(naviBar)
        naviBar.items = [navItem]
        
        //设置 navBar 的渲染颜色
        naviBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        
        //设置标题颜色
        naviBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
        
        //设置系统按钮的文字渲染颜色  只对系统.plain 的方法有效
        naviBar.tintColor = UIColor.orange
    }
}

extension LSBaseViewController: UITableViewDelegate, UITableViewDataSource {
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}
